import zipfile, os  

class TreeNode(object):

    def __init__(self, name, isDirectory, isOpen = False):
        self.name=name
        self.isDirectory = isDirectory
        self.isOpen = isOpen
        self.children = []

    def getChild(self,name):
        for node in self.children :
            if node.name == name :
                return node
        return None

    def getChildren(self) :
        return self.children

    def addChild(self,child):
        self.children.append(child)

    def renderToString(self,depth, drawText, vertMap, isLastChild):
        treeParts = ''
        if depth > 1 :
            for j in vertMap[0:-1] :
                if j == 1 :
                    treeParts = treeParts + "| "
                else :
                    treeParts = treeParts + " "
        if isLastChild :
            treeParts = treeParts + "'"
        else :
            treeParts = treeParts + "|"

        if self.isDirectory :
            if self.isOpen :
                treeParts = treeParts + "~"
            else :
                treeParts = treeParts + "+"
        else :
            treeParts = treeParts + "-"

        treeParts = treeParts + self.name + "\n"

        if self.isDirectory and self.isOpen :
            childNodes = self.getChildren()
            if len(childNodes) > 0 :
                lastIndex = len(childNodes) -1 
                if lastIndex > 0 :
                    for i in range(0,lastIndex):
                        tmpMap = vertMap[:]
                        tmpMap.append(1)
                        treeParts = treeParts + childNodes[i].renderToString(depth+1, 1, tmpMap, 0)
                tmpMap = vertMap[:]
                tmpMap.append(0)
                treeParts = treeParts + childNodes[lastIndex].renderToString(depth+1, 1, tmpMap, 1)
        return treeParts

class ZipTree(object):

    def __init__(self,zip_file_path):
        self.zip_file_path = zip_file_path

    def add_tree_entry(self,root, line):
        sections = line.split("/")
        parentNode = root

        for index, item in enumerate(sections):
            if item == "" :
                continue
            sameChild = parentNode.getChild(item)
            if sameChild == None :
                isDirectory = True
                if index == len(sections)-1 and not line.endswith("/"):
                    isDirectory = False
                node = TreeNode(item, isDirectory,isOpen=True)
                parentNode.addChild(node)
                parentNode = node
            else :
                parentNode = sameChild

    def build_zip_tree(self):
        zipFile = zipfile.ZipFile(self.zip_file_path)  
        root = TreeNode(self.zip_file_path,isDirectory=True, isOpen=True)
        root.isDirectory = True
        root.isOpen = True
        for name in zipFile.namelist() :
            self.add_tree_entry(root,name)
        return root

    def renderToString(self):
        root = self.build_zip_tree()
        result = root.renderToString(0,0, [],0)
        return result

if __name__ == "__main__" :

    ziptree = ZipTree("commons-io-1.4.jar")
    print ziptree.renderToString()
    
    
